/**
 * 
 */
angular.module('app.service.catagory', ['ngResource'])

//catagory
    .service('CatagoryService', ['$resource', function ($resource) {
        var resource = $resource(localhost + "catagory/:cataid", {}, {
            get: {cache: true, method: 'GET'},
            query: {cache: true, method: 'GET', isArray: true}
        });
        var catatory_id_name_map = {};
        var catatory_name_id_map = {};
        
        //获取列表
        this.getlist = function () {
            return resource.query({}, function(list){
                for (var i=0; i<list.length; i++) {
                    var catagory = list[i];
                    catatory_id_name_map[catagory.cataid] = catagory.name;
                    catatory_name_id_map[catagory.name] = catagory.cataid;
                }
            });
        }
        // get cataid by cataname
        this.getcataidbyname = function (name) {
            if (catatory_name_id_map[name]) {
                return catatory_name_id_map[name];
            }
            return null;
        }
        // get cataname by cataid
        this.getcatanamebyid = function (cataid) {
            if (catatory_id_name_map[cataid]) {
                return catatory_id_name_map[cataid];
            }
            return null;
        }
        //通过id,获取
        this.getbyid = function (id) {
            return resource.get({cataid: id});
        }
        //通过name,获取
        this.getbyname = function (name) {
            var resource = $resource(localhost + "catagory/byname/:name");
            return resource.get({name: name});
        }
    }])
;